home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Softshoe / Lisa's Mac Parts / TextEdit / TextEditObject.cp < prev    next >
Text File  |  2000-06-23  |  3KB  |  130 lines

  1. // TextEditObject.cp
  2.  
  3. #ifndef TextEditObject_h
  4. #include "TextEditObject.h"
  5. #endif
  6. #ifndef Overflow_h
  7. #include "Overflow.h"
  8. #endif
  9. #ifndef MemoryFullError_h
  10. #include "MemoryFullError.h"
  11. #endif
  12. #ifndef TooLargeForTextEdit_h
  13. #include "TooLargeForTextEdit.h"
  14. #endif
  15.  
  16. TextEditObject::TextEditObject( GrafPortObject& port,
  17.                                           const Rect& view,
  18.                                           uint16 wrapWidth )
  19.   {
  20.     Assert( port.IsCurrent() );
  21.     
  22.     Rectangle destination( view );
  23.     Assert( CanAddSigned( destination.left, wrapWidth ) );
  24.     destination.right = destination.left + wrapWidth;
  25.     
  26.     te = TENew( &destination, &view );
  27.     
  28.     if ( te == 0 )
  29.         throw MemoryFullError();
  30.     
  31.     Assert( *te != 0 );
  32.     Assert( Record().hText != 0 );
  33.     Assert( *Record().hText != 0 );
  34.   }
  35.  
  36. void TextEditObject::SetWrapWidth( uint16 wrapWidth )
  37.   {
  38.     Rectangle& destination( Destination() );
  39.     Assert( CanAddSigned( destination.left, wrapWidth ) );
  40.     destination.right = destination.left + wrapWidth;
  41.   }
  42.  
  43. void TextEditObject::SetScreenArea( const Rectangle& newView )
  44.   {
  45.     Rectangle& destination( Destination() );
  46.     Rectangle& view( View() );
  47.  
  48.     destination += newView.TopLeft() - view.TopLeft();
  49.     view = newView;
  50.   }
  51.  
  52. void TextEditObject::DisplayAt( GrafPortObject& port,
  53.                                           Rectangle area,
  54.                                           PointObject scroll )
  55.   {
  56.     View() = area;
  57.     uint32 wrapWidth = WrapWidth();
  58.     Destination() = Rectangle( area.left - scroll.h,
  59.                                         area.top - scroll.v,
  60.                                         area.left - scroll.h + wrapWidth,
  61.                                         area.bottom );
  62.     (*te)->inPort = &port;
  63.   }
  64.  
  65. PointObject TextEditObject::Scroll() const
  66.   {
  67.     return PointObject( View().TopLeft() - Destination().TopLeft() );
  68.   }
  69.  
  70. void TextEditObject::SetScroll( PointObject newScroll )
  71.   {
  72.     PointObject offset = newScroll - Scroll();
  73.     if ( offset != PointObject::zero )
  74.         TEScroll( offset.h, offset.v, te );
  75.   }
  76.  
  77. void TextEditObject::ReplaceSelection( ConstData text )
  78.   {
  79.     if ( text.Length() > maxint16
  80.           || Length() - Selection().Length() + text.Length() > maxint16 )
  81.         throw TooLargeForTextEdit();
  82.     
  83.     if ( !Selection().IsEmpty() )
  84.         DeleteSelection();
  85.     
  86.     TEInsert( text.Start(), text.Length(), te );
  87.   }
  88.  
  89. void TextEditObject::Type( uint8 key )
  90.   {
  91.     if ( Length() >= maxint16
  92.           && key != backspace
  93.           && key != leftArrow
  94.           && key != rightArrow
  95.           && key != upArrow
  96.           && key != downArrow )
  97.         throw TooLargeForTextEdit();
  98.         
  99.     TEKey( key, te );
  100.   }
  101.  
  102. void TextEditObject::SetText( ConstData text )
  103.   {
  104.     if ( text.Length() > maxint16 )
  105.         throw TooLargeForTextEdit();
  106.      TESetText( text.Start(), text.Length(), te );
  107.   }
  108.  
  109. void TextEditObject::DeleteForward()
  110.   {
  111.     URange16 selection = Selection();
  112.     
  113.     if ( !selection.IsEmpty() )
  114.         DeleteSelection();
  115.      else
  116.         if ( selection.Start() < Length() )
  117.           {
  118.             SetSelection( URange16( selection.Start()+1,
  119.                                             selection.Start()+1 ) );
  120.             DeleteBackward();
  121.           }
  122.   }
  123.  
  124. void TextEditObject::SetFace( const ::Face& face )
  125.   {
  126.     SetFont( face.Font() );
  127.     SetSize( face.Size() );
  128.     SetStyle( face.Style() );
  129.   }
  130.